'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Windows.Forms
Imports System.IO
Imports BigPondUsage

Public Class Telstra
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)
        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String
            Dim usage As New BigPondUsage.Usage(ConfigurationData.Username, ConfigurationData.Password)
            usage.BrowseToPage("monthly")
            strHTMLData = usage.GetHtml(1)
            'strHTMLData = My.Computer.FileSystem.ReadAllText("C:\programming\isp\telstra2\ViewMonthlyUsage.htm", System.Text.Encoding.ASCII)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            If strHTMLData = "" Then
                ISPReturnData.ErrorMessage = "Unknown Error in BigPondUsage Class"
                Exit Sub
            End If

            Dim strTemp As String
            Dim strlines() As String
            Dim Hours As Integer
            Dim Minutes As Integer
            Dim Seconds As Integer
            Dim Wireless As Boolean = False
            Dim TimeLimit As Boolean = False
            Dim CostPerMeg As Single
            Dim CostPerHour As Single
            Dim AdditionalCost As String = "$0"

            strTemp = ExtractText(strHTMLData, "Monthly Plan Allowance", 0, "<td>", "</td>")
            strTemp = Replace(strTemp, ",", "")

            'check for timelimit
            If strTemp.ToLower.Contains("hours per month") = True Then TimeLimit = True

            'get plan type
            strTemp = ExtractText(strHTMLData, "Current Plan", 0, "<td>", "</td>")

            'check for wireless
            If strTemp.ToLower.Contains("wireless") = True Then
                Wireless = True

                'check for laptop
                If strTemp.ToLower.Contains("laptop") = True Then
                    If TimeLimit = True Then
                        CostPerHour = 9.6
                    Else
                        CostPerMeg = 0.3
                    End If
                Else
                    CostPerMeg = 0.15
                End If
            End If

            strTemp = ExtractText(strHTMLData, "Current Account Usage", 0, "<strong>", "</strong>")
            strTemp = Replace(strTemp, ",", "")

            If Wireless = True Then
                If TimeLimit = True Then
                    strlines = strTemp.Split(":".ToCharArray)
                    Hours = CInt(Val(strlines(0)))
                    Minutes = CInt(Val(strlines(1)))
                    Seconds = CInt(Val(strlines(2)))
                    ISPReturnData.PeakDownload = CSng(Hours + (Minutes / 60) + ((Seconds / 60) / 60))
                Else
                    ISPReturnData.PeakDownload = CSng(Val(strTemp))
                    'convert to MB if required
                    If InStr(strTemp, "GB") > 0 Then ISPReturnData.PeakDownload = CSng(Val(ISPReturnData.PeakDownload) * 1000)
                End If
            Else
                ISPReturnData.PeakDownload = CSng(Val(strTemp))
                'convert to MB if required
                If InStr(strTemp, "GB") > 0 Then ISPReturnData.PeakDownload = CSng(Val(ISPReturnData.PeakDownload) * 1000)
            End If

            strTemp = ExtractText(strHTMLData, "Current Bill Period", 0, "bottom"">", " - ")

            'Telstra doesn't give the exact update time, so use now instead
            ISPReturnData.LastUpdated = Now

            'calculate costs
            If Wireless = True Then
                If ISPReturnData.PeakDownload > Val(ConfigurationData.PeakUsageLimit) Then
                    If TimeLimit = True Then
                        AdditionalCost = "$" & Format((ISPReturnData.PeakDownload - Val(ConfigurationData.PeakUsageLimit)) * CostPerHour, "#0.00").ToString
                    Else
                        AdditionalCost = "$" & ((ISPReturnData.PeakDownload - Val(ConfigurationData.PeakUsageLimit)) * CostPerMeg).ToString
                    End If
                End If
            End If

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data

            'usage
            If Wireless = False Then
                ISPReturnData.GraphItemList.Add(New GraphItem("Usage " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))
            Else
                If TimeLimit = True Then
                    ISPReturnData.GraphItemList.Add(New GraphItem("Usage " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ConvertToTime(ISPReturnData.PeakDownload) & ")", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))
                Else
                    ISPReturnData.GraphItemList.Add(New GraphItem("Usage " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))
                End If
            End If

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            If Wireless = False Then
                ISPReturnData.TextItemList.Add(New TextItem("Usage Left :", ISPReturnData.PeakUsageRemainingDescription))
                If Not String.IsNullOrEmpty(ISPReturnData.UsageDeltaDescription) Then
                    ISPReturnData.TextItemList.Add(New TextItem("Usage Delta :", ISPReturnData.UsageDeltaDescription))
                End If
                ISPReturnData.TextItemList.Add(New TextItem("Usage Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))

                If ISPReturnData.ShapedNow = False Then
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
                Else
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
                End If
            Else
                If TimeLimit = True Then
                    ISPReturnData.PeakUsageRemainingDescription = ConvertToTime(CSng(Val(ConfigurationData.PeakUsageLimit)) - ISPReturnData.PeakDownload)
                    If ISPReturnData.DaysLeft > 0 Then
                        ISPReturnData.PeakUsageRemainingPerDayDescription = ConvertToTime((CSng(Val(ConfigurationData.PeakUsageLimit)) - ISPReturnData.PeakDownload) / ISPReturnData.DaysLeft) & "/Day (" & ISPReturnData.DaysLeft & " Days)"
                    Else
                        ISPReturnData.PeakUsageRemainingPerDayDescription = ConvertToTime(CSng(Val(ConfigurationData.PeakUsageLimit)) - ISPReturnData.PeakDownload) & " /Day (" & ISPReturnData.DaysLeft & " Days)"
                    End If
                    ISPReturnData.PeakUsageRemainingPerDayDescription = Replace(ISPReturnData.PeakUsageRemainingPerDayDescription, "hr ", ":")
                    ISPReturnData.PeakUsageRemainingPerDayDescription = Replace(ISPReturnData.PeakUsageRemainingPerDayDescription, "min ", ":")
                    ISPReturnData.PeakUsageRemainingPerDayDescription = Replace(ISPReturnData.PeakUsageRemainingPerDayDescription, "sec", "hr")

                    ISPReturnData.TextItemList.Add(New TextItem("Time Left :", ISPReturnData.PeakUsageRemainingDescription))
                    ISPReturnData.TextItemList.Add(New TextItem("Time Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                Else
                    ISPReturnData.TextItemList.Add(New TextItem("Usage Left :", ISPReturnData.PeakUsageRemainingDescription))
                    ISPReturnData.TextItemList.Add(New TextItem("Usage Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                End If
                ISPReturnData.TextItemList.Add(New TextItem("Additional Cost :", AdditionalCost))
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try

    End Sub

    Function ConvertToTime(ByVal DecTime As Single) As String
        Dim Hours As Integer
        Dim Minutes As Integer
        Dim Seconds As Integer
        Hours = CInt(Math.Floor(DecTime))
        Minutes = CInt(Math.Floor((DecTime - Hours) * 60))
        Seconds = CInt(((DecTime - Hours - (Minutes / 60)) * 60 * 60))
        Return (Hours & "hr " & Minutes & "min " & Seconds & "sec")
    End Function

End Class
